<?php

namespace Admin\Controller;

class LoadAreaController extends AdminController {

    public function index() {

        $keywords = I('keywords');
        $where['la.is_del'] = 0;
        if ($keywords) {
            $where['loadarea_name'] = array('like', "%{$keywords}%");
            $this->assign('keywords', $keywords);
        }

        $M = M()->table("ot_load_area as la");
        $field = 'la.*';
        $order = "la.loadarea_id desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('_list', $list);
        $this->display();
    }

    public function add() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            D()->startTrans();
            try {
                $loadarea_name = I('loadarea_name');
                $pid = I('pid', 0, 'intval');
                $cid = I('cid', 0, 'intval');
                if (empty($loadarea_name)) {
                    $this->error('请填写装货区域名称');
                }

                if ($pid == 0) {
                    $this->error('请选择省');
                }

                if ($cid == 0) {
                    $this->error('请选择市');
                }


                $ldata['loadarea_name'] = $loadarea_name;
                $ldata['province'] = M('District')->where(['id' => $pid])->getField('name');
                $ldata['city'] = M('District')->where(['id' => $cid])->getField('name');

                if ($id == 0) {
                    $id = M('LoadArea')->add($ldata);
                } else {
                    M('LoadArea')->where(array('loadarea_id' => $id))->save($ldata);
                }

                $polist = M('Port')->select();
                foreach ($polist as $val) {

                    $ppid = I($val['port_id'] . '-ppid', 0, 'intval');

                    $freight_list['small_carpool'] = I($val['port_id'] . "-freight-small_carpool", 0);
                    $freight_list['small_single'] = I($val['port_id'] . "-freight-small_single", 0);
                    $freight_list['big_cabinet'] = I($val['port_id'] . "-freight-big_cabinet", 0);
                    $freight_list['tall_cabinet'] = I($val['port_id'] . "-freight-tall_cabinet", 0);
                    $freight_list['super_tall_cabinet'] = I($val['port_id'] . "-freight-super_tall_cabinet", 0);

                    $cash_list['small_carpool'] = I($val['port_id'] . "-cash-small_carpool", 0);
                    $cash_list['small_single'] = I($val['port_id'] . "-cash-small_single", 0);
                    $cash_list['big_cabinet'] = I($val['port_id'] . "-cash-big_cabinet", 0);
                    $cash_list['tall_cabinet'] = I($val['port_id'] . "-cash-tall_cabinet", 0);
                    $cash_list['super_tall_cabinet'] = I($val['port_id'] . "-cash-super_tall_cabinet", 0);

                    $settlement_list['small_carpool'] = I($val['port_id'] . "-settlement-small_carpool", 0);
                    $settlement_list['small_single'] = I($val['port_id'] . "-settlement-small_single", 0);
                    $settlement_list['big_cabinet'] = I($val['port_id'] . "-settlement-big_cabinet", 0);
                    $settlement_list['tall_cabinet'] = I($val['port_id'] . "-settlement-tall_cabinet", 0);
                    $settlement_list['super_tall_cabinet'] = I($val['port_id'] . "-settlement-super_tall_cabinet", 0);

                    $ppdata['port_id'] = $val['port_id'];
                    $ppdata['loadarea_id'] = $id;
                    $ppdata['freight_list'] = json_encode($freight_list);
                    $ppdata['cash_list'] = json_encode($cash_list);
                    $ppdata['settlement_list'] = json_encode($settlement_list);

                    if ($ppid == 0) {
                        M('PortPrice')->add($ppdata);
                    } else {
                        M('PortPrice')->where(['port_price_id' => $ppid])->save($ppdata);
                    }
                }

                D()->commit();
                $this->success('执行成功!', U('index'));
            } catch (Exception $ex) {
                D()->rollback();
                $this->error('执行失败!');
            }
        } else {
            if ($id != 0) {
                $data = M('LoadArea')->where(array('loadarea_id' => $id))->find();
                $province = M('District')->where(['name' => $data['province']])->getField('id');
                $clist = M('District')->where(['upid' => $province])->select();

                $lalist = M()->table("ot_load_area as la")
                        ->field('la.*,pp.port_price_id,pp.port_price_id,pp.freight_list,pp.cash_list,pp.settlement_list,p.port_name,p.port_id')
                        ->join('ot_port_price as pp on la.loadarea_id = pp.loadarea_id')
                        ->join('ot_port as p on pp.port_id = p.port_id')
                        ->where(['la.loadarea_id' => $id])
                        ->select();
                foreach ($lalist as &$val) {
                    $val['freight_list'] = (array) json_decode($val['freight_list']);
                    $val['cash_list'] = (array) json_decode($val['cash_list']);
                    $val['settlement_list'] = (array) json_decode($val['settlement_list']);
                }
                unset($val);

                $this->assign('lalist', $lalist);
                $this->assign('clist', $clist);
                $this->assign('data', $data);
            }
            $polist = M('Port')->select();
            $this->assign('polist', $polist);
            $plist = M('District')->field('id,name')->where(['upid' => 0])->select();
            $this->assign('plist', $plist);
            $this->display();
        }
    }

    public function getCityByPid() {
        $id = I('id', 0, 'intval');
        $dlist = M('District')->field('id,name')->where(['upid' => $id])->select();
        $this->success('获取成功', $dlist);
    }

    public function del() {
        $id = I('id', 0, 'intval');

        $data['is_del'] = 1;
        M('LoadArea')->where(['loadarea_id' => $id])->save($data);
        $this->success('删除成功');
    }

}
